﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using socialnexus.classes.models;
using socialnexus.classes.pages;

namespace socialnexus.admin.Administrators
{
    public partial class Edit : ProtectedPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AdminUser su = AdminUser.GetById(Request.QueryString["UserId"].ToString());
            if (su == null)
            {
                Response.Redirect("~/admin/Administrators/");
                Response.Close();
            }
            else
            {
                if (!IsPostBack)
                {
                    hfUserId.Value = Request.QueryString["UserId"].ToString();
                    lblUserName.Text = su.Username;
                    txtFirstName.Text = su.FirstName;
                    txtLastName.Text = su.LastName;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Page.Validate("UserData");
            if (Page.IsValid)
            {
                AdminUser au = AdminUser.GetById(hfUserId.Value);
                au.FirstName = txtFirstName.Text;
                au.LastName = txtLastName.Text;

                au.Update();
                Response.Redirect("~/admin/Administrators/Show.aspx?UserId=" + au.AdminUserId);
                Response.Close();
            }
        }


        protected void btnPasswordChange_Click(object sender, EventArgs e)
        {
            Page.Validate("Password");
            if (Page.IsValid)
            {
                AdminUser au = AdminUser.GetById(hfUserId.Value);
                if (!String.IsNullOrEmpty(txtPassword.Text) &&
                    !String.IsNullOrEmpty(txtPassword.Text) &&
                    txtPassword.Text == txtPasswordConfirm.Text)
                {
                    au.Password = txtPassword.Text;
                    au.Update();

                    Response.Redirect("~/admin/Administrators/Show.aspx?UserId=" + au.AdminUserId);
                    Response.Close();
                }
                else
                {
                    lblPasswordError.Visible = true;
                    lblPasswordError.Text = "Passwords do not match.";
                }
            }
        }

        protected void ValidatePassword(object sender, ServerValidateEventArgs e)
        {
            e.IsValid = (txtPassword.Text == txtPasswordConfirm.Text);
        }
    }
}
